"""
Case Type   : datakit安全中心
Case Name   : 删除角色
Create At   : 2025/2/7
Owner       : @sungang14
Description :
    1. 点击安全中心-账号管理
    2. 点击“添加账号”
    3. 输入必须的字段，账号，密码，用户昵称，手机号，输入备注；状态设为停用；
    4. 点击确定【结果1】
    5、退出当前账号，使用新创建账号登录【结果2】
Expect      :
    1、【结果1】新账号成功创建
    2、【结果2】该账号无法登录
History     :
"""

import os
import unittest
import json
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.Api import Api


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.assertNotEqual("error", self.token)
        self.sys_api = Api(self.token)
        self.user_name = 'sgtest_user0030'
        self.user_passwd = f"Huawei@123"
        self.data_user = f"userName=" + self.user_name
        text = '----查询待添加账号是否存在，存在则删除----'
        self.log.info(text)
        res1 = self.sys_api.query_user_page(self.data_user)
        self.log.info(res1.json())
        self.num_user = res1.json()['total']
        self.log.info(self.num_user)
        if self.num_user != 0:
            self.user_id = res1.json()['rows'][0]['userId']
            res6 = self.sys_api.delete_user([self.user_id])
            self.log.info(res6.json())
            self.assertEqual(200, res6.json()['code'], '删账号失败')
            self.assertEqual("success", res6.json()['msg'], '删除账号失败')

    def test_batch_installation(self):
        text = '----step1:安全中心-账号管理，点击“添加账号”，输入必须的字段，账号，密码，用户昵称，手机号，输入备注；状态设为停用；点击确定 expect:成功----'
        self.log.info(text)
        self.data3 = {"userName": self.user_name,
                      "password": f"sBfBGhAg4+NypfvYDSAdyvZPx/PAW0NInwVdN3IGNHfj62ltrvonFdhGFEti8MIRajnN0mlbUsUktMOlgIHBWih4ffAEaDcCI+uN1jrQAigQcrOLwNZHkb3pRfQyelWpx2A5LgEhuGmJ6vPN13DSd+C3D5uMGZhGG7BEQPSGomk=",
                      "nickName": self.user_name,
                      "phonenumber": f"16666666666",
                      "roleIds": '',
                      "status": "1"}
        res3 = self.sys_api.add_user(self.data3)
        self.log.info(res3.json())
        self.assertEqual(200, res3.json()['code'], '添加账号失败')
        self.assertEqual("success", res3.json()['msg'], '添加账号失败')

        text = '----step2:退出当前账号，使用新创建账号登录 expect:该账号无法登录----'
        self.log.info(text)
        self.token1 = self.com_sh.get_token_value(password=self.user_passwd, user=self.user_name)
        self.log.info(self.token1)
        self.assertEqual("error", self.token1, "该账号可以登陆")

    def tearDown(self):
        text = '----:查询账号user_id expect:查询成功----'
        self.log.info(text)
        res5 = self.sys_api.query_user_page(self.data_user)
        self.log.info(res5.json())
        self.assertEqual(1, res5.json()['total'], '查询账号失败')
        self.user_id = res5.json()['rows'][0]['userId']
        self.log.info(self.user_id)
        text = '----:删除账号 expect：删除成功----'
        self.log.info(text)
        self.data6 = [self.user_id]
        res6 = self.sys_api.delete_user(self.data6)
        self.log.info(res6.json())
        self.assertEqual(200, res6.json()['code'], '删账号失败')
        self.assertEqual("success", res6.json()['msg'], '删除账号失败')
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
